package com.hcinfos.gsoo.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.config.Constants;
import com.hcinfos.gsoo.enity.Coupon;
import com.hcinfos.gsoo.util.ImageUtil;
import com.hcinfos.gsoo.util.LogUtil;
import com.hcinfos.gsoo.util.ImageUtil.ImageCallback;

public class ShopCouponAdapter extends BaseAdapter {

	private List<Coupon> list;
	private LayoutInflater mInflater;
	public int index = 0;
	public Context context;
	public boolean isVip = false;

	public ShopCouponAdapter(Context context, List<Coupon> list) {
		this.list = list;
		this.context = context;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final TypeHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.coupon_list_item, null);
			holder = new TypeHolder();
			holder.nameTv = (TextView) convertView
					.findViewById(R.id.shop_title);
			holder.icon_img = (ImageView) convertView
					.findViewById(R.id.shop_img);
			holder.price_tv = (TextView) convertView
					.findViewById(R.id.shop_price_num);
			holder.address_tv = (TextView) convertView
					.findViewById(R.id.shop_preferential);
			holder.type_img = (ImageView) convertView
					.findViewById(R.id.shop_icon_quan);
			convertView.setTag(holder);
		} else {
			holder = (TypeHolder) convertView.getTag();
		}
		Coupon coupon = list.get(position);
		String name = coupon.getActName();
		Double actVal = coupon.getActVal();
		Integer actValType = coupon.getActValType();
		Integer num = coupon.getActNum();
		String intr = coupon.getActDes();
		String str = null;
		if (!isVip) {
			if (actValType == 0) {
				str = "优惠:" + actVal + " 剩余:" + num;
			} else {
				str = "折扣:" + actVal + " 剩余:" + num;
			}
		} else {
			str = "折扣:" + actVal + " 剩余:" + num;
		}

		try {
		} catch (Exception e) {
			LogUtil.d("wxf", "CouponAdapter" + e.toString());
		}
		if (intr.length() > 20) {
			intr = intr.substring(0, 20) + "...";
		} else {
			intr = coupon.getActDes();
		}
		holder.nameTv.setText(name);
		holder.price_tv.setText(str);
		holder.address_tv.setText(intr);
		String url = Constants.SERVER + coupon.getImgAddr()
				+ Constants.IMG_THUMB_DIR + coupon.getImg() + ".jpg";
		ImageUtil.setThumbnailView(url, holder.icon_img, context,
				new ImageCallback() {
					@Override
					public void loadImage(Bitmap bitmap, String imagePath) {
						holder.icon_img.setImageBitmap(bitmap);
					}
				}, true, false);
		String actType = coupon.getActType();
		if (actType.equals("coupon")) {
			holder.type_img.setImageResource(R.drawable.tc_icon_quan);
		} else {
			// holder.type_img.setImageResource(R.drawable.flag_ka);
		}
		return convertView;
	}

	private final class TypeHolder {
		TextView nameTv;
		ImageView icon_img;
		TextView price_tv;
		TextView address_tv;
		ImageView type_img;

	}

	public void setDataChange(List<Coupon> list) {
		this.list = list;
		notifyDataSetChanged();
	}

}
